﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Zyg.Utility.Helper;
using Zyg.SSO.Core.Business;

namespace Zyg.SSO.Web.Admin.App
{
    public partial class ModifyApp : Zyg.SSO.Web.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!base.Account.HavePermission(Permission.AppManage))
                {
                    throw new Exception("你无权查看此页面");
                }
                bool parseSucceed;

                Guid id = Request.QueryString["id"].ToGuid(out parseSucceed);

                if (parseSucceed)
                {
                    BindData(id);
                }
                else
                {
                    throw new Exception("对不起，你所访问的页面不存在");
                }
            }
        }

        protected void BindData(Guid id)
        {
            Zyg.SSO.Core.Business.App app = Zyg.SSO.Core.Business.App.Load(id);

            if (app == null)
            {
                throw new Exception("对不起，你所访问的页面不存在");
            }

            AppSSOId.Text = app.Id.ToString();
            AppIP.Text = app.IP;
            AppName.Text = app.Name;
            AppPort.Text = app.Port.ToString();
            AppUrl.Text = app.Url;
            AppLoginFileAddress.Text = app.LoginFile;

            if (!string.IsNullOrEmpty(app.PictureAddress))
            {
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(Server.MapPath(app.PictureAddress));

                if (fileInfo.Exists)
                {
                    AppPic.ImageUrl = app.PictureAddress;
                }
                else
                {
                    AppPic.ImageUrl = "~/Content/Account/no_head.gif";
                }
            }
            else
            {
                AppPic.ImageUrl = "~/Content/Account/no_head.gif";
            }
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            bool parseSucceed;

            Guid id = Request.QueryString["id"].ToGuid(out parseSucceed);

            Zyg.SSO.Core.Business.App app = Zyg.SSO.Core.Business.App.Load(id);

            app.IP = AppIP.Text;
            app.LoginFile = AppLoginFileAddress.Text;
            app.Name = AppName.Text;

            if (String.IsNullOrEmpty(AppPort.Text))
            {
                app.Port = 80;
            }
            else
            {
                app.Port = int.Parse(AppPort.Text);
            }

            app.Url = AppUrl.Text;

            if (ModifyPicHidden.Value.Equals("true") && FileAppPicutre.HasFile)
            {
                string accountContentPath = Server.MapPath("~/Content/App/");
                string extension = Zyg.Utility.Helper.FileHelper.GetFileExtension(FileAppPicutre.FileName);

                string fileName = app.Id.ToString() + extension;

                app.PictureAddress = "~/Content/App/" + fileName;

                FileAppPicutre.SaveAs(accountContentPath + fileName);
            }

            app.Save();

            Zyg.SSO.Core.Business.Log log = new Zyg.SSO.Core.Business.Log();
            log.AccountId = base.Account.Id;
            log.ModuleId = Zyg.SSO.Core.Business.Module.App.Id;
            log.Message = string.Format("管理员 {0} 修改了应用系统 {1}", base.Account.Email, app.Name);

            log.Save();

            BindData(id);
        }
    }
}
